<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/03/10
//      |_|                                         | TpMeCMS

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\store;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;

use ImiApp\ApiServer\Api\Model\Goodssku;

use ImiApp\ApiServer\Api\Model\Goods as FaGoods;
use ImiApp\ApiServer\Api\Model\GoodsskuPrice;
// use ImiApp\ApiServer\Common\Store\StoreAuth;
// * @StoreAuth(name="token",nologin={"no"})

/**
 * 店铺商品
 * @Auth(name="token",nologin={"no"})
 * @Controller("/store.goods/")
 */
class Goods extends StoreOwner
{


    // protected $whereCom;



    // public function _initialize()
    // {
    //     parent::_initialize();
    //     $this->whereCom['store_id'] = $this->store_id;
    // }





    /**
     * 创建商品
     *
     * @param string $title 商品标题
     * @param string $subtitle 副标题
     * @param string $goodscate_ids 商品分类id
     * @param string $content 商品详情介绍
     * @param string $dispatch_ids 发货模板
     * @param string $dispatch_type 发货方式:express=物流快递,selfetch=用户自提,store=商家配送,autosend=自动发货,service=上门核销'
     * @param string $image 商品主图
     * @param string $images 商品图集
     * @param string $content_images 商品详细图集
     * @param string $is_sku 是否多规格0=否1=是
     * @param string $params 规格参数[{"title":"品牌","content":"xiaomi"},{"title":"产地","content":"中国"}]
     * @param string $original_price 原价
     * @param string $price 价格
     * @param string $type 商品类型:normal=实体商品,virtual=虚拟商品,service=服务
     * @param string $show_sales 显示销量
     * @param string $service_ids 服务标签id
     * @param string $video_image 视频封面
     * @param string $video_file 视频文件
     * @param string $goodsbrand_id 品牌id
     * @param string $send_addr 发货地址
     * @param string $expire_time 到期时间
     * @param string $new_status 商品类型:new=全新,unused=闲置,soon=临期
     * @param string $listJson 规格参数[{id,name,pid,weigh,temp_id,children:[id,name,pid,weigh,temp_id]}]
     * @param string $priceJson 价格参数{"id":141,"goods_sku_ids":"139,142","goods_id":40,"weigh":0,"image":"","stock":10,"stock_warning":null,"sales":0,"sn":"A1","weight":160,"price":"1.00","goods_sku_text":["测试黑色","256G"],"status":"up","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":1,"goods_sku_temp_ids":[2,5]}
     * @return void
     * @Action
     */
    public function create_goods(
        $title = null,
        $subtitle = null,
        $goodscate_ids = null,
        $content = null,
        $dispatch_ids = '1',
        $dispatch_type = 'express',
        $image = null,
        $images = null,
        $content_images = null,
        $is_sku = null,
        $params = null,
        $original_price = null,
        $price = null,
        $type = 'normal',
        $show_sales = 0,
        $stock = 0,
        $sn = '',
        $listJson = '',
        $priceJson = '',
        $service_ids = null,
        $video_image = null,
        $video_file = null,
        $goodsbrand_id = null,
        $send_addr = null,
        $expire_time = null,
        $new_status = 'new'
    ) {

        $this->init();
        $this->isRun(); //是否可以继续以下操作
        $this->isAddGoods();
        $param = $this->request->post();

        // var_dump($param,$param['is_sku']);
        if (!$param['is_sku']) {
            // 单规格，price 必须是数字
            if (!preg_match('/^[0-9]+(.[0-9]{1,8})?$/', $param['price'])) {
                return $this->error("请填写正确的价格");
            }
        }

        $this->isGoodscateIds($goodscate_ids); //判断店铺是否有该商品的资格
        $data['goodscate_ids'] = $goodscate_ids;
        $data['goodsbrand_id'] = $goodsbrand_id;
        $data['send_addr'] = $send_addr; //发货地址
        $data['content'] = $this->Xss($content);
        $data['content_images'] = $content_images;

        $data['dispatch_ids'] = $dispatch_ids; //发货模板
        $data['dispatch_type'] = $dispatch_type; //发货方式:express=物流快递,selfetch=用户自提,store=商家配送,autosend=自动发货,service=上门核销',
        // $data['expire_day'] = '0';
        $data['image'] = $image;
        $data['images'] = $images;
        $data['is_sku'] = $is_sku; //是否多规格 0=否1=是
        $data['original_price'] = $original_price; //原价
        $data['params'] = $this->Xss($params);
        // $data['params_arr][0][title'] = '品牌';
        // $data['params_arr][0][content'] = 'iphone';
        // $data['params_arr][1][title'] = '产地';
        // $data['params_arr][1][content'] = '中国';
        $data['price'] = $price; //价格
        $data['service_ids'] = $service_ids; //服务标签
        $data['show_sales'] = $show_sales; //显示销量
        $data['status'] = 'up';
        $data['subtitle'] = $this->Xss($subtitle);
        $data['title'] = $this->Xss($title);
        $data['type'] = $type; //商品类型
        // $data['views'] = '10'; //浏览人数
        // $data['weigh'] = '999999';
        // $data['weight'] = 0;
        // $data['stock'] = $stock;
        // $data['stock_warning_switch'] = 'false';
        // $data['sn'] = $sn;
        // $data['autosend_content'] = '';
        $data['store_id'] = $this->store_id; //店铺ID;
        $data['createtime'] = time();
        $data['updatetime'] = time();
        $data['apply_status'] = '1'; //审核状态:0=未知,1=申请中,2=已通过,3=已拒绝


        $data['video_file'] = $video_file;
        if ($video_file && !$video_image) {
            return $this->error('请上传视频封面', null,);
        }
        $data['video_image'] = $video_image;
        $data['new_status'] = $new_status;
        $data['expire_time'] = $expire_time;




        Db::getInstance()->beginTransaction();
        // try {
        $data['goods_service_ratio'] = $this->StoreGetGoodsMoneyServerRatio($data, $this->store->set_id, $this->store_id); //卖家服务费比率

        $in_data = get_field_arr($data, [
            'goodscate_ids',
            'goodsbrand_id',
            'content',
            'dispatch_ids',
            'dispatch_type',
            'image',
            'images',
            'is_sku',
            'original_price',
            'params',
            'price',
            'service_ids',
            'show_sales',
            'subtitle',
            'title',
            'type',
            'content_images',
            'send_addr',
            'new_status',
            'expire_time',
            'status',
            'video_file',
            'video_image',


            // 'stock',
            // 'stock_warning_switch',
            // 'sn',
            'store_id',
            'createtime',
            'updatetime',
            'apply_status'
        ]);

        $goods = FaGoods::newInstance();
        // $goods->set($data);
        // foreach ($data as $field => $val) {
        //     try {
        //         $field = 'set'.\Imi\Util\Text::toCamelName($field);
        //         $goods->$field($val);
        //     } catch (\Throwable $th) {
        //         //throw $th;
        //     }
        // }
        $goods->insert($in_data);
        // var_dump('$goods->id',$goods->id);
        $data['id'] = $goods->id;
        // $data['id'] = FaGoods->insert($data)->getLastInsertId();

        $sku['listJson'] = $listJson;
        $sku['priceJson'] = $priceJson;
        // $sku['listJson'] = '[{"id":138,"name":"测试颜色","pid":0,"goods_id":40,"weigh":0,"children":[{"id":139,"name":"测试黑色","pid":138,"goods_id":40,"weigh":0,"temp_id":2},{"id":140,"name":"白色","pid":138,"goods_id":40,"weigh":0,"temp_id":3}],"temp_id":1},{"id":141,"name":"容量","pid":0,"goods_id":40,"weigh":0,"children":[{"id":142,"name":"256G","pid":141,"goods_id":40,"weigh":0,"temp_id":5},{"id":143,"name":"128G","pid":141,"goods_id":40,"weigh":0,"temp_id":6}],"temp_id":4}]';
        // $sku['priceJson'] = '[{"id":141,"goods_sku_ids":"139,142","goods_id":40,"weigh":0,"image":"","stock":10,"stock_warning":null,"sales":0,"sn":"A1","weight":160,"price":"1.00","goods_sku_text":["测试黑色","256G"],"status":"up","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":1,"goods_sku_temp_ids":[2,5]},{"id":142,"goods_sku_ids":"140,142","goods_id":40,"weigh":0,"image":"","stock":20,"stock_warning":null,"sales":0,"sn":"A2","weight":160,"price":"2.00","goods_sku_text":["白色","256G"],"status":"up","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":2,"goods_sku_temp_ids":[3,5]},{"id":143,"goods_sku_ids":"139,143","goods_id":40,"weigh":0,"image":"","stock":30,"stock_warning":null,"sales":0,"sn":"A3","weight":160,"price":"3.00","goods_sku_text":["黑色","128G"],"status":"up","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":3,"goods_sku_temp_ids":[2,6]},{"id":144,"goods_sku_ids":"140,143","goods_id":40,"weigh":0,"image":"","stock":4,"stock_warning":null,"sales":0,"sn":"A4","weight":160,"price":"4.00","goods_sku_text":["白色","128G"],"status":"down","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":4,"goods_sku_temp_ids":[3,6]}]';

        $this->editSku($data, $sku, 'add');

        Db::getInstance()->commit();
        // } catch (\Throwable $e) {
        //     Db::getInstance()->rollBack();
        //     return $this->error($e->getMessage());
        // }
        return $this->success('创建成功,等待审核', $data);
    }

    /**
     * 获取我的商品列表
     *
     * @param integer $page 页1
     * @param integer $limit 数量10
     * @param string $search 搜索
     * @param string $type 商品类型:normal=实体商品,virtual=虚拟商品,service=服务
     * @param string $status 商品状态:up=上架,hidden=隐藏商品,down=下架
     * @param string $apply_status 审核状态:1=申请中,2=已通过,3=已拒绝,4=未通过审核的
     * @return void
     * @Action
     */
    public function get_goods_list(?int $page = 1, ?int $limit = 10, $apply_status = null, $search = null, $type = null, $status = null, $params = null)
    {
        $this->init();


        // $this->store->id;
        $page = $page ?: 1;
        $limit = $limit ?: 10;
        $where['store_id'] = $this->store_id;
        if ($apply_status){
            
            if($apply_status=='4'){
                $where['apply_status'] = ['in',['0','1','3']];
            }else{
                $where['apply_status'] = $apply_status;
            }
        
        }
            
        if ($search)
            $where['title|subtitle|content'] = ['like', '%' . $search . '%'];
        if ($type)
            $where['type'] = $type;
        if ($status)
            $where['status'] = $status;
        // if($params)
        //     $where['params'] = $params;
        $list = Db::name('goods')

            // ->whereEx($this->whereCom)

            ->whereEx($where)
            ->whereIsNull('deletetime')
            ->orderRaw('updatetime desc,createtime desc')
            ->page($page, $limit)
            ->select()
            ->getArray();
        if ($list) {
            foreach ($list as $key => &$val) {
                // $this->ChangGoodsField($val);
            }
        }
        return $this->success('', $list);
    }

    /**
     * 编辑商品
     *
     * @param int $id
     * @param string $title 商品标题
     * @param string $subtitle 副标题
     * @param string $goodscate_ids 商品分类id
     * @param string $content 商品详情介绍
     * @param string $dispatch_ids 发货模板
     * @param string $dispatch_type 发货方式:express=物流快递,selfetch=用户自提,store=商家配送,autosend=自动发货,service=上门核销'
     * @param string $image 商品主图
     * @param string $images 商品图集
     * @param string $content_images 商品详细图集
     * @param string $is_sku 是否多规格0=否1=是
     * @param string $params 规格参数[{"title":"品牌","content":"xiaomi"},{"title":"产地","content":"中国"}]
     * @param string $original_price 原价
     * @param string $price 价格
     * @param string $type 商品类型:normal=实体商品,virtual=虚拟商品,service=服务
     * @param string $show_sales 显示销量
     * @param string $service_ids 服务标签id
     * @param string $video_image 视频封面
     * @param string $video_file 视频文件
     * @param string $goodsbrand_id 品牌id
     * @param string $send_addr 发货地址
     * @param string $expire_time 到期时间
     * @param string $new_status 商品类型:new=全新,unused=闲置,soon=临期
     * @param string $listJson 规格参数[{id,name,pid,weigh,temp_id,children:[id,name,pid,weigh,temp_id]}]
     * @param string $priceJson 价格参数{"id":141,"goods_sku_ids":"139,142","goods_id":40,"weigh":0,"image":"","stock":10,"stock_warning":null,"sales":0,"sn":"A1","weight":160,"price":"1.00","goods_sku_text":["测试黑色","256G"],"status":"up","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":1,"goods_sku_temp_ids":[2,5]}

     * @return void
     * @Action
     */
    public function edit_goods(
        $id,
        $title = null,
        $subtitle = null,
        $goodscate_ids = null,
        $content = null,
        $dispatch_ids = null,
        $dispatch_type = 'express',
        $image = null,
        $images = null,
        $content_images = null,
        $is_sku = null,
        $params = null,
        $original_price = null,
        $price = null,
        $type = null,
        $show_sales = null,
        $stock = null,
        $sn = null,
        $listJson = null,
        $priceJson = null,
        $service_ids = null,
        $video_image = null,
        $video_file = null,
        $goodsbrand_id = null,
        $send_addr = null,
        $expire_time = null,
        $new_status = null
    ) {
        $this->init();
        $this->isRun(); //是否可以继续以下操作

        $where['id'] = $id;
        $where['store_id'] = $this->store_id;
        $goods = Db::name('goods')
            // ->whereEx($this->whereCom)
            ->whereEx($where)->select()->get();
        if (!$goods) {
            return $this->error('没有该商品');
        }

        $param = $this->request->param();
        if (!$goods['is_sku']) {
            // 单规格，price 必须是数字
            if (!preg_match('/^[0-9]+(.[0-9]{1,8})?$/', $param['price'])) {
                return $this->error("请填写正确的价格");
            }
        }

        if ($goodscate_ids)
            $this->isGoodscateIds($goodscate_ids); //判断店铺是否有该商品的资格
        if ($goodscate_ids)
            $data['goodscate_ids'] = $goodscate_ids;
        if ($goodsbrand_id)
            $data['goodsbrand_id'] = $goodsbrand_id;
        if ($send_addr)
            $data['send_addr'] = $send_addr;
        if ($content)
            $data['content'] = $this->Xss($content);
        if ($content_images)
            $data['content_images'] = $content_images;

        if ($dispatch_ids)
            $data['dispatch_ids'] = $dispatch_ids; //发货模板
        if ($dispatch_type)
            $data['dispatch_type'] = $dispatch_type; //发货方式:express=物流快递,selfetch=用户自提,store=商家配送,autosend=自动发货,service=上门核销',
        // $data['expire_day'] = '0';
        if ($image)
            $data['image'] = $image;
        if ($images)
            $data['images'] = $images;

        // $data['is_sku'] = $is_sku; //是否多规格 0=否1=是
        if ($original_price)
            $data['original_price'] = $original_price; //原价
        if ($params)
            $data['params'] = $this->Xss($params);
        // $data['params_arr][0][title'] = '品牌';
        // $data['params_arr][0][content'] = 'iphone';
        // $data['params_arr][1][title'] = '产地';
        // $data['params_arr][1][content'] = '中国';
        if ($price)
            $data['price'] = $price; //价格
        if ($service_ids)
            $data['service_ids'] = $service_ids; //服务标签
        // if($show_sales)
        // $data['show_sales'] = $show_sales; //显示销量
        // $data['status'] = 'up';
        if ($subtitle)
            $data['subtitle'] = $this->Xss($subtitle);
        if ($title)
            $data['title'] = $this->Xss($title);
        if ($type)
            $data['type'] = $type; //商品类型
        // $data['views'] = '10'; //浏览人数
        // $data['weigh'] = '999999';
        // $data['weight'] = 0;
        // $data['stock'] = $stock;
        // $data['stock_warning_switch'] = 'false';
        // $data['sn'] = $sn;
        // $data['autosend_content'] = '';
        // $data['store_id'] = $this->store_id; //店铺ID;
        // $data['createtime'] = time();
        $data['updatetime'] = time();
        $data['apply_status'] = '1'; //审核状态:0=未知,1=申请中,2=已通过,3=已拒绝

        if($video_file)
            $data['video_file'] = $video_file;
        if ($video_file && !$video_image) {
            return $this->error('请上传视频封面', null,);
        }
        if ($video_image)
            $data['video_image'] = $video_image;
        $data['new_status'] = $new_status;
        if ($expire_time)
            $data['expire_time'] = $expire_time;




        Db::getInstance()->beginTransaction();
        try {
            $data['goods_service_ratio'] = $this->StoreGetGoodsMoneyServerRatio($data, $this->store->set_id, $this->store_id); //卖家服务费比率


            $data['id'] = $id;
            // unset($data['id']);

            Db::name('goods')->where('id', '=', $id)->update($data); //->strict(false)
            // $data['id'] = $id;
            // var_dump('475', $data);
            $sku['listJson'] = $listJson;
            $sku['priceJson'] = $priceJson;
            // $sku['listJson'] = '[{"id":138,"name":"测试颜色","pid":0,"goods_id":40,"weigh":0,"children":[{"id":139,"name":"测试黑色","pid":138,"goods_id":40,"weigh":0,"temp_id":2},{"id":140,"name":"白色","pid":138,"goods_id":40,"weigh":0,"temp_id":3}],"temp_id":1},{"id":141,"name":"容量","pid":0,"goods_id":40,"weigh":0,"children":[{"id":142,"name":"256G","pid":141,"goods_id":40,"weigh":0,"temp_id":5},{"id":143,"name":"128G","pid":141,"goods_id":40,"weigh":0,"temp_id":6}],"temp_id":4}]';
            // $sku['priceJson'] = '[{"id":141,"goods_sku_ids":"139,142","goods_id":40,"weigh":0,"image":"","stock":10,"stock_warning":null,"sales":0,"sn":"A1","weight":160,"price":"1.00","goods_sku_text":["测试黑色","256G"],"status":"up","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":1,"goods_sku_temp_ids":[2,5]},{"id":142,"goods_sku_ids":"140,142","goods_id":40,"weigh":0,"image":"","stock":20,"stock_warning":null,"sales":0,"sn":"A2","weight":160,"price":"2.00","goods_sku_text":["白色","256G"],"status":"up","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":2,"goods_sku_temp_ids":[3,5]},{"id":143,"goods_sku_ids":"139,143","goods_id":40,"weigh":0,"image":"","stock":30,"stock_warning":null,"sales":0,"sn":"A3","weight":160,"price":"3.00","goods_sku_text":["黑色","128G"],"status":"up","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":3,"goods_sku_temp_ids":[2,6]},{"id":144,"goods_sku_ids":"140,143","goods_id":40,"weigh":0,"image":"","stock":4,"stock_warning":null,"sales":0,"sn":"A4","weight":160,"price":"4.00","goods_sku_text":["白色","128G"],"status":"down","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":4,"goods_sku_temp_ids":[3,6]}]';
            if ($is_sku) {
                $data['is_sku'] = $is_sku;
            }
            $this->editSku($data, $sku, 'edit');

            Db::getInstance()->commit();
        } catch (\Throwable $e) {
            Db::getInstance()->rollBack();
            return $this->error($e->getMessage());
        }
        return $this->success('编辑成功,等待审核', $data);
    }

    /**
     * 更新商品状态
     *
     * @param int $id 商品id
     * @param string $status 状态up=上架,hidden=隐藏商品,down=下架
     * @return void
     * @Action
     */
    public function update_goods_status($id, $status)
    {
        $this->init();
        $this->isRun(); //是否可以继续以下操作

        $where['id'] = $id;
        $where['store_id'] = $this->store_id;
        $goods = Db::name('goods')
            // ->whereEx($this->whereCom)
            ->whereEx($where)
            ->select()
            ->get();
        if (!$goods) {
            return $this->error('没有该商品');
        }
        switch ($status) { //商品状态:up=上架,hidden=隐藏商品,down=下架
            case 'up':
                // 上架 判断是否审核通过
                if ($goods['apply_status'] != '2')
                    return $this->error('审核状态未通过');
                break;
            case 'hidden':
                //隐藏商品 
                break;
            case 'down':
                //下架 判断云仓什么的
                break;
            default:
                # code...
                break;
        }
        $data['status'] = $status;
        $data['updatetime'] = time();

        Db::name('goods')
            // ->whereEx($this->whereCom)
            ->whereEx($where)->update($data);
        return $this->error('修改成功');
    }

    /**
     * 获取商品详情
     *
     * @param int $id 商品id
     * @return void
     * @Action
     */
    public function get_goods_detail(?int $id)
    {
        $this->init();
        $where['id'] = $id;
        $where['store_id'] = $this->store_id;
        $goods = Db::name('goods')
            // ->whereEx($this->whereCom)
            ->whereEx($where)
            ->select()
            ->get();
        if ($goods)
            $goods = $this->ChangGoodsField($goods);
        return $this->success('goods', $goods);
    }

    /**
     * 删除商品
     *
     * @param string|int $id 商品id
     * @return void
     * @Action
     */
    public function del_goods($id)
    {
        $this->init();
        $this->isRun(); //是否可以继续以下操作

        if(is_numeric($id)){
            $where['id'] = $id;
        }else{
            $where['id'] = ['in',explode(',',$id)];
          
        }
        $where['store_id'] = $this->store_id;
        $list = Db::name('goods')
            // ->whereEx($this->whereCom)
            ->whereEx($where)->whereIsNull('deletetime')->select()->getArray();
        if (!$list) {
            return $this->error('没有该商品');
        }
        foreach ($list as $key => $goods) {
            $where2['id'] = $goods['id'];

            $res = Db::name('goods')
            // ->whereEx($this->whereCom)
            ->whereEx($where2)->update(['deletetime' => time(), 'status' => 'down']);
        }
        //删除判断 云仓等数据
        
        return $this->success('删除成功');
    }




    protected function ChangGoodsField($goods)
    {
        $goods['goodscate'] = Db::name('goodscate')->where('id', '=', $goods['goodscate_ids'])->find();
        $goods['p_goodscate'] = Db::name('goodscate')->where('id', '=', $goods['goodscate']['pid'])->find();
        if ($goods['new_status'] == 'soon')
            $goods['expire_date'] = date('Y-m-d H:i', $goods['expire_time']);
        if ($goods['is_sku']) {
            $skuList = Goodssku::dbQuery()->whereEx(['pid' => 0, 'goods_id' => $goods['id']])->select()->getArray();
            // var_dump($skuList);
            if ($skuList) {

                foreach ($skuList as $key => &$s) {
                    // var_dump($s);
                    $s['children'] = Goodssku::dbQuery()->whereEx(['pid' => $s['id'], 'goods_id' => $goods['id']])->select()->getArray();

                    // $s->children = Goodssku::dbQuery()->whereEx(['pid' => $s->id, 'goods_id' => $goods['id']])->select()->getArray();
                    // var_dump($skuList[$key]['children']);

                }
            }
            $goods['skuList'] = $skuList;
            var_dump($goods['skuList']);


            $skuPrice = GoodsskuPrice::dbQuery()->whereEx(['goods_id' => $goods['id']])->select()->getArray();
            $goods['skuPrice'] = $skuPrice;
        } else {
            // 将单规格的部分数据直接放到 goods 上
            $goodsSkuPrice = GoodsskuPrice::dbQuery()->whereEx(['goods_id' => $goods['id']])->orderRaw('id asc')->select()->get();

            $goods['stock'] = $goodsSkuPrice['stock'] ?? 0;
            $goods['sn'] = $goodsSkuPrice['sn'] ?? "";
            $goods['weight'] = $goodsSkuPrice['weight'] ?? 0;
            $goods['stock_warning'] = $goodsSkuPrice['stock_warning'];

            $goods['skuList'] = [];
            $goods['skuPrice'] =  $goodsSkuPrice;
        }


        return $goods;
    }
}
